
*********************************
*Create income ranks
*********************************


*Dowload ready-made data

use "D:\ready-made\FOLK_vl_7085\vl7085_1.dta" , clear


destring ika, replace
keep shnro tyotu svatv vuosi ika


*Only 1970 and 1975 incomes

preserve
keep if vuosi==1975
rename tyotu tyotu_75
rename svatv svatv_75
*deflate by cost of living index to 1970
replace tyotu_75=tyotu_75/1.66
replace svatv_75=svatv_75/1.66
gen byear3=vuosi-ika
drop vuosi ika
tempfile i75
save `i75'
restore

keep if vuosi==1970

gen byear=vuosi-ika

keep if byear>1904&byear<1940

drop vuosi

joinby shnro using `i75',unmatched(both) 

drop _merge

replace byear=byear3 if byear==.

keep if byear>1904&byear<1940
drop byear3 

rename tyotu tyotu_70
rename svatv svatv_70

egen tyotu=rowmean(tyotu_70 tyotu_75)
egen svatv=rowmean(svatv_70 svatv_75)


*earnings 

gen ln_tyotu=ln(tyotu)
gen ln_tyotu2=ln(tyotu)

*income 

gen ln_svatv=ln(svatv)
gen ln_svatv2=ln(svatv+1)

forvalues x=1905/1939 {

egen rank`x'=rank(svatv) if byear==`x', unique
egen rank_`x'=cut(rank`x') if byear==`x', group(100)
drop rank`x'
}

gen income_rank=.

forvalues x=1905/1939 {

replace income_rank=rank_`x' if byear==`x'
drop rank_`x'
}

save "W:\Matti M\1970_1975_income_ranks_2.dta", replace

erase `i75'

clear all

*********************************
*-- Create family link in the 1950 census data
*********************************

use  "W:\Matti M\vl50.dta" , clear

gen perhekoodi=PASKO50+PRUNO50
drop if PASKO50=="000000"

destring PIKA50, gen(age50)
drop PIKA50

label var age50 "Age in 1950"
destring age50, replace
gen byear=1950-age50

*rename asunro asunro50
*label var asunro "asuntokunta"
rename PRUNO50 pruno50
label var pruno50 "ruokakunta"
rename PENO50 peno50
label var peno50 "perhe"
rename ASRU50 asru50
label var asru50 "asema ruokakunnassa"
destring asru50, replace
label def asru50 0 "päämies", add
label def asru50 1 "puoliso", add
label def asru50 2 "lapsi", add
label def asru50 3 "lapsenlapsi", add
label def asru50 4 "vanhemmat (pääiehen/puolison)", add
label def asru50 5 "muu sukulainen", add
label def asru50 6 "palvelusväki", add
label def asru50 7 "muu", add
label def asru50 8 "laitoshoidossa", add
label def asru50 9 "tuntematon", add
label val asru50 asru50 

rename PEAS50 peas50
destring peas50, replace
label var peas50 "perheasema"
label def peas50 0 "ei kuulu perheväestöön, add
label def peas50 1 "avio- tai avopuoliso", add
label def peas50 2 "äiti tai isä", add
label def peas50 3 "lapsi", add
label val peas50 peas50

rename PETY50 pety50
destring pety50, replace
label var pety50 "perhetyyppi"
label def pety50 0 "ei kuulu perheväestöön, tuntematon" 1 "aviopuolisot, ei lapsia" /*
*/ 2 "aviopuolisot, lapsia" 3 "nainen ja lapsia" 4 "mies ja lapsia", add
label val pety50 pety50

rename SIVSA50 marstat50
destring marstat50, replace
label var marstat50 "Marital status"
label def marstat50 1 "Single" 2 "Married" 3 "Divorced" 4 "Separated" 5 "Widow" 9 "Unknown"
label val marstat50 marstat50

rename sp fem
destring fem, replace
replace fem=fem-1
 

rename TOLA50 ind50
label var ind50 "Industry in 1950"
destring ind50, replace


rename AKUNU50 mun50
label var mun50 "Municipality of residence, 1950"
rename sykunu mun_birth
label var mun_birth "Municipality of birth"
rename AKUNU39 mun39
label var mun39 "Municipality of residence, 1939"

rename AMAS50 occstat50
label var occstat50 "Occupational status 1950 (AMAS50)"
destring occstat50, replace
label def occstat50 0 "Entrepeneur with employees" 1 "Entrepeneur wo/ employees" 2 "Manager" 3 "White-collar worker" 4 "Blue-collar worker" ///
	5 "Ass fam: white-collar" 6 "Ass fam: blue-collar"  7 "Ass fam: white or blue" 9 "Out of labor force"
label val occstat50 occstat50
tab occstat50


rename AMAS39 occstat39
label var occstat39 "Occupational status 1939 (AMAS39)"
destring occstat39, replace
label def occstat39  1 "Entrepeneur" 2 "Manager" 3 "White-collar worker" 4 "Blue-collar worker" ///
	5 "Ass fam: white-collar" 6 "Ass fam: blue-collar"  9 "Out of labor force"
label val occstat39 occstat39
tab occstat39

**Assisting family also into blue-collar and white-collar

replace occstat39=3 if occstat39==5
replace occstat39=4 if occstat39==6

gen blue_collar=(occstat50==4)
gen unemployed=(occstat50==9)

rename KOULU50 school50
label var school50 "Type of school in 1950"
replace school50="99" if school50=="X"
destring school50, replace
label def school50 0 "Kansakoulu" 1 "Oppikoulu" 2 "Korkeakoulu" 3 "Kansan- ja ty򶤥nopistot" ///
		4 "Kansakouluopettajaseminaarit" 5 "MMK oppilaitokset" 6 "Kauppaoppilaitokset" 7 "Tekniset oppilaitokset" ///
		8 "Muut ammatilliset" 9 "Muut, tuntematon" 99 "not in school", add
label val school50 school50		
tab school50 if school50!=99

rename SITA50 edu50
destring edu50, replace
label var edu50 "Education in 1950"
label def edu50 1 "Kansakoulu" 2 "Keskikoulu" 3 "Ylioppilas" 9 "None, unknown", add
label val edu50 edu50
tab byear edu50

rename TOLA39 ind39
label var ind39 "Industry in 1939"
destring ind39, replace
gen agri39=(ind39==1)
gen primary39=(ind39<6)


preserve 
*Head of household
keep if asru50==0&fem==0

keep shnro  perhekoodi   JNRO50 age50  mun39   edu50         

tempfile head
save `head'
restore 

*********************************
*Find the Father
*********************************

preserve 
use svatv income_rank shnro using "W:\Matti M\1970_1975_income_ranks_2.dta", clear
drop if shnro==""
joinby shnro using `head', unmatched(using)

keep  perhekoodi   svatv income_rank     edu50       age50  

foreach x  in  svatv income_rank    edu50 age50   {
rename `x' `x'_head
}

duplicates drop perhekoodi svatv_head income_rank_head edu50_head , force

save  "W:\Matti M\1970_1975_income_ranks_head.dta", replace 

restore 



*********************************
*Find the children
*********************************

keep JNRO50 perhekoodi asru50 shnro

keep if asru50==2 

*********************************
*Link within the family
*********************************

count
joinby perhekoodi using  "W:\Matti M\1970_1975_income_ranks_head.dta"
count

tab edu50_head, gen(eh_)

gen kansa_edu_head=eh_1
gen keski_edu_head=eh_2
gen yli_edu_head=eh_3
gen no_edu_head=eh_4

keep JNRO50 *head 

drop if JNRO50==""


save "W:\Matti M\father_link_1950_UM.dta", replace

